package com.cjjd.c.feature.mine.information;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ToggleButton;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.StringUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/8/18.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ContactAddFragment extends ActionBarFragment {


    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_email)
    EditText etEmail;
    @Bind(R.id.toggleButton)
    ToggleButton toggleButton;

    @Override
    public Object getTitleString() {
        return "新增常用联系人";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_add_contact;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void initRightButton(Button rightButton) {
        super.initRightButton(rightButton);
//        rightButton.setText("保存");
//        rightButton.setVisibility(View.VISIBLE);
    }


    @OnClick({R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_next:
                if (checkCanSave()) saveContactInfo();
                break;
        }
    }

    private String name;
    private String mobile;
    private String email;
    private String idDefault;

    private boolean checkCanSave() {
        name = etName.getText().toString().trim();
        mobile = etPhone.getText().toString().trim();
        email = etEmail.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            ToastUtil.showToast("请输入姓名");
            return false;
        }
        if (!StringUtil.isMobile(mobile)) {
            ToastUtil.showToast("请输入正确的手机号码");
            return false;
        }
        if (!StringUtil.isEmail(email)) {
            ToastUtil.showToast("请输入正确的邮箱");
            return false;
        }

        if (toggleButton.isChecked()) {
            idDefault = "1";
        } else {
            idDefault = "0";
        }
        return true;
    }

    private void saveContactInfo() {

        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("name", name);           // 联系人姓名
        h1.put("mobile", mobile);         //手机号码
        h1.put("email", email);          //邮箱
        h1.put("isDefault", idDefault);      //是否默认联系人默认0否1是

        Call call = service.addMemberContact(JsonUtils.convertParam("addMemberContact", h1));
        call.enqueue(new CallbackImpl() {
            @Override
            public void responseSuccess(Call call, Response response) {
                ToastUtil.showToast("添加成功");
                mContext.onBackPressed();
            }
        });

    }
}
